package org.ejc.util;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.ejc.exception.EJCPersistenceException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 * Utility class which will contain many useful methods.
 * <p>
 * Will probably be a collection of static methods.
 * </p>
 * @author zac
 *
 */
public final class EJCUtils {

	/**
	 * Get the HibernateSession from JNDI tree.
	 * @return Hopefully a valid hibernate session.
	 * @throws EJCPersistenceException Thrown if there is any problems in getting the hibernate session.
	 */
	public static Session getHibernateSession()throws EJCPersistenceException{
		Session hsession=null;
		try{
		InitialContext ctx = new InitialContext();
		SessionFactory factory = (SessionFactory)ctx.lookup("java:/hibernate/EjcSessionFactory");
		hsession= factory.openSession();
		}catch(NamingException ne){
			throw new EJCPersistenceException("Error in getting Hibernate Session",ne);
		}
		return hsession;
	}
	
	
}
